package com.sskj.gocoin.v.activity;

import android.content.Context;
import android.content.Intent;
import android.support.constraint.Guideline;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hey.lib.HeySpinner;
import com.sskj.gocoin.R;
import com.sskj.gocoin.R2;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.common.MoneyValueFilter;
import com.sskj.gocoin.common.MyTextWatcher;
import com.sskj.gocoin.common.widget.ToolBarLayout;
import com.sskj.gocoin.m.bean.CoinEntry;
import com.sskj.gocoin.m.bean.ExchangeCoin;
import com.sskj.gocoin.m.bean.UserInfo;
import com.sskj.gocoin.p.activity.ExchangePresenter;
import com.sskj.gocoin.utils.ClickUtil;
import com.sskj.gocoin.utils.NumberUtil;
import com.sskj.gocoin.utils.ToastUtil;
import com.sskj.gocoin.v.base.BaseActivity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 兑换
 */
public class ExchangeActivity extends BaseActivity<ExchangePresenter> {
    @BindView(R2.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R2.id.guideline13)
    Guideline guideline13;
    @BindView(R2.id.select_unit2)
    HeySpinner selectUnit2;
    @BindView(R2.id.select_unit1)
    HeySpinner selectUnit1;
    @BindView(R2.id.exchange_num)
    EditText exchangeNum;
    @BindView(R2.id.unit1)
    TextView unit1;
    @BindView(R2.id.asset_num)
    TextView assetNum;
    @BindView(R2.id.unit2)
    TextView unit2;
    @BindView(R2.id.password)
    EditText password;
    @BindView(R2.id.submit)
    Button submit;

    private double rate;

    private String coin;
    private String ex_coin;

    private UserInfo userInfo;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_exchange;
    }

    @Override
    public ExchangePresenter getPresenter() {
        return new ExchangePresenter();
    }

    @Override
    protected void initView() {
        toolbar.setLeftButtonOnClickLinster(v -> {
            finish();
        });
        //兑换历史
        toolbar.setRightButtonOnClickLinster(v -> {
            startActivity(new Intent(this, ExchangeHistoryActivity.class));
        });
        exchangeNum.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                calcExchange();
            }
        });
        exchangeNum.setFilters(new InputFilter[]{new MoneyValueFilter(4)});
        //兑换
        ClickUtil.click(submit, () -> {
            if (!checkIdent()) {
                return;
            }

            if (userInfo != null) {
                if (userInfo.getConfig().getPayment_password_set()==0) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.strLevelTransactionFragmentX279));
                    startActivity(new Intent(mActivity, PayPwdSettingActivity.class));
                    return;
                }
            }

            if (TextUtils.isEmpty(exchangeNum.getText()) || Double.parseDouble(exchangeNum.getText().toString()) == 0) {
                ToastUtil.showShort(getString(R.string.input_exchange_num));
                return;
            }
            if (TextUtils.isEmpty(password.getText())) {
                ToastUtil.showShort(getString(R.string.xmlactivity_pay_pwd_input32));
                return;
            }
            mPresenter.exchange(coin, ex_coin, exchangeNum.getText().toString(), password.getText().toString());
        });
    }

    private void calcExchange() {
        if (!TextUtils.isEmpty(exchangeNum.getText())) {
            BigDecimal num = new BigDecimal(exchangeNum.getText().toString()).multiply(new BigDecimal(rate));
            assetNum.setText(num.toString());
        } else {
            assetNum.setText("0");
        }
    }

    @Override
    protected void initData() {
        //获取用户信息
        mPresenter.getUserInfo();
        //获取可兑换币种
        mPresenter.getExchangeCoinList();
    }

    /**
     * 币种的兑换列表
     *
     * @param data
     */
    public void setExchangeCoinData(List<ExchangeCoin> data) {
        List<String> coinList = new ArrayList<>();
        for (ExchangeCoin coin : data) {
            coinList.add(coin.getEx_name());
        }
        selectUnit2.setOnSelectListener(position -> {
            unit2.setText(data.get(position).getEx_name());
            rate = data.get(position).getEx_rate();
            ex_coin = data.get(position).getEx_coin();
        });
        selectUnit2.attachData(coinList);

    }

    /**
     * 可兑换币种
     *
     * @param data
     */
    public void setExchangeAbleList(List<CoinEntry> data) {
        List<String> coinList = new ArrayList<>();
        for (CoinEntry coin : data) {
            coinList.add(coin.getPname());
        }
        selectUnit1.setOnSelectListener(position -> {
            mPresenter.getCoinList(data.get(position).getCode());
            unit1.setText(data.get(position).getPname());
            coin = data.get(position).getCode();
            exchangeNum.getText().clear();
            password.getText().clear();
        });
        selectUnit1.attachData(coinList);
    }

    public void submitSuccess(boolean isSuccess) {
        password.getText().clear();
        if (isSuccess) {
            exchangeNum.setText("");
            password.setText("");
            startActivity(new Intent(this, ExchangeHistoryActivity.class));
        }
    }

    public void updateUserInfo(UserInfo data) {
        this.userInfo = data;
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, ExchangeActivity.class);
        context.startActivity(intent);
    }
}
